﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CRMWeb.Pages.Test
{
    public partial class TestCompoundIntrestCalculator : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button4_Click(object sender, EventArgs e)
        {
            Calc_EMI();
            Calc_int();
        }

        public void Calc_EMI()
        {
            double Last_inst, x, i9, q18 = 472.46, q19 = 0;
            i9 = q18;
            Last_inst = q18;
            x = i9 / 10;
            while (x >= 0.01)
            {
                while (q19 < 0.15)
                {
                    Last_inst = i9;
                    i9 = (Last_inst - x);
                }
                i9 = Last_inst;
                x = x / 10;
            }
            //  txtLoanAmount.Text = i9.ToString();
        }

        public void Calc_int()
        {
            double x, q24, i5 = 0.5, i2 = 14, q21 = 1522.17, INTT = 0;
            int i = 0;
            q24 = i5 * i2 / 365 / 2;
            x = 0.01;
            while (x > 0.0000000005)
            {
                while (q21 > 0.0001)
                {
                    if (i > 40)
                        break;
                    INTT = q24 + x;
                    q24 = INTT;
                    i = i + 1;
                }
                q24 = INTT - x;
                x = x / 10;
            }
            //txtLoanAmount.Text = q24.ToString();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            int noofdays = Int32.Parse(txtNumberofDays.Text);
            decimal principal = decimal.Parse(txtPrincipal.Text);
            int loanduration = Int32.Parse(txtLoanDuration.Text);
            decimal intrestrate = decimal.Parse(txtInterestRate.Text);
            DateTime firstrepaymentdate = DateTime.Parse(txtFirstRepaymentDate.Text);
            DateTime lastrepaymentdate = DateTime.Parse(txtLastRepaymentDate.Text);
            int numberofinstallments = Int32.Parse(txtNoofInstallments.Text);
            decimal repayment = decimal.Parse(txtRepayment.Text);
            int daysinyear = Int32.Parse(txtDaysinayear.Text);
            int compareResult;


            DataTable dtSchedule = new DataTable();
            dtSchedule.Columns.Add("Next Interest Date", typeof(DateTime));
            dtSchedule.Columns.Add("Next Repayment Date", typeof(DateTime));
            dtSchedule.Columns.Add("DueDate", typeof(DateTime));
            dtSchedule.Columns.Add("Repayment", typeof(decimal));
            dtSchedule.Columns.Add("InterestTotal", typeof(decimal));
            dtSchedule.Columns.Add("Balance", typeof(decimal));
            dtSchedule.Columns.Add("Intrest", typeof(decimal));
            DataRow dr = dtSchedule.NewRow();
            dr["DueDate"] = DateTime.Now.Date;
            dr["Next Repayment Date"] = firstrepaymentdate;
            var lastDayOfMonth = DateTime.Now.AddMonths(1).AddDays(1 - DateTime.Now.Day).Date;
            var nextintrestdate = lastDayOfMonth.AddDays(-1);
            dr["Next Interest Date"] = nextintrestdate;
            dr["InterestTotal"] = principal;
            dr["Balance"] = principal;
            dtSchedule.Rows.Add(dr);
            var lastDayOfMonth1 = DateTime.Now.AddMonths(1).AddDays(1 - DateTime.Now.Day).Date;
            DateTime temp;
            var lastDayOfMonth2 = DateTime.Now.AddMonths(1).AddDays(1 - DateTime.Now.Day).Date;
            object o, o1;
            for (int i = 0; i < numberofinstallments - 1; i++)
            {

                DataRow dr1 = dtSchedule.NewRow();

                //for (int k = 0; k < dtSchedule.Rows.Count; k++)
                //    for (int j = 0; j < dtSchedule.Columns.Count; j++)
                //    {

                //if you want to get the string
                //string s = o = dt.Rows[i].ItemArray[j].ToString();
                //}

                if (i == 0)
                {
                    compareResult = firstrepaymentdate.CompareTo(nextintrestdate);
                }
                else
                {
                    compareResult = firstrepaymentdate.CompareTo(lastDayOfMonth1);
                }

                if (compareResult < 0)
                {
                    dr1["DueDate"] = firstrepaymentdate;
                    temp = firstrepaymentdate;
                    //firstrepaymentdate = temp;
                }
                else
                {
                    dr1["DueDate"] = nextintrestdate;
                    temp = nextintrestdate;
                }
                //DateTime duedate = DateTime.Parse(dr1["DueDate"].ToString());

                //var lastDayOfMonth1 = temp.AddMonths(1).AddDays(1 - temp.Day).Date;
                //var nextintrestdate1 = lastDayOfMonth1.AddDays(-1);

                //DateTime now = DateTime.Now;

                //DateTime lastDayLastMonth = new DateTime(temp.Year, temp.Month, 1);
                //lastDayLastMonth = lastDayLastMonth.AddDays(-1);

                //DateTime now = DateTime.Now;
                //DateTime lastDayOfLastMonth = now.Date.AddDays(-temp.Day);
                if (i == 0)
                {
                    lastDayOfMonth1 = temp.AddMonths(1);
                }
                else
                {
                    lastDayOfMonth1 = temp.AddMonths(1).AddDays(1 - temp.Day).Date;
                    lastDayOfMonth1 = lastDayOfMonth1.AddDays(-1);
                }
                //var temp2=lastDayOfMonth1.
                //var nextintrestdate1= lastDayOfMonth1.AddDays(-1);



                dr1["Next Interest Date"] = lastDayOfMonth1;
                if (lastDayOfMonth1 == lastDayOfMonth2)
                {
                    dr1["Next Repayment Date"] = firstrepaymentdate.AddDays(7);
                }
                else
                {
                    dr1["Next Repayment Date"] = firstrepaymentdate;
                }
                lastDayOfMonth2 = lastDayOfMonth1;
                if (i == 0)
                {
                    dr1["Repayment"] = 0.00;
                }
                else
                {
                    dr1["Repayment"] = repayment;
                }
                
                //DateTime d1 = DateTime.Now; ;
                //DateTime d2 = lastDayOfMonth;
                if (i == 0)
                {
                    o = dtSchedule.Rows[i];
                    o1 = dtSchedule.Rows[i].ItemArray[0];
                }
                else
                {
                    //o = dtSchedule.Rows[i - 1].ItemArray[2];
                    o = temp;
                    o1 = dtSchedule.Rows[i].ItemArray[2];
                }

                DateTime d1;
                DateTime d2;
                if (i == 0)
                {
                    d1 = DateTime.Now.Date;
                    //d2 = lastDayOfMonth;
                    d2 = DateTime.Parse(o1.ToString());
                }
                else
                {
                    d1 = DateTime.Parse(o.ToString());
                    d2 = DateTime.Parse(o1.ToString());
                }

                TimeSpan t = d2 - d1;
                decimal NoOfDays = decimal.Parse(t.TotalDays.ToString());
                NoOfDays = Math.Round(NoOfDays, 0);
                //decimal NoOfDays=double.Parse((d1 - d2).TotalDays);
                decimal intrest = Math.Round(((NoOfDays + 1) / daysinyear * (intrestrate / 100) * principal), 2);

                dr1["Intrest"] = Math.Abs(intrest);
                dr1["Balance"] = principal + intrest;

                dr1["Balance"] = principal - repayment;

                principal = principal + intrest;
                dr1["InterestTotal"] = intrest;
                

                dtSchedule.Rows.Add(dr1);
            }
            //var repaymentList = new List<RepaymentDetail>();
            //DataRow dr = dtSchedule.NewRow();
            //dr["DueDate"] = DateTime.Now.Date;
            //dr["Balance"] = principal;

            //dr["Repayment"] = repayment;
            //dr["Principal"] = principal;
            //dtSchedule.Rows.Add(dr);

            //var anyDt = DateTime.Now;
            //var lastDayOfMonth = anyDt.AddMonths(1).AddDays(1 - anyDt.Day).Date;
            //lastDayOfMonth = lastDayOfMonth.AddDays(-1);

            //DataRow dr1 = dtSchedule.NewRow();
            //dr1["DueDate"] = lastDayOfMonth;
            //dr1["Balance"] = principal;
            //dr1["Interest"] = principal;
            //dr1["Repayment"] = repayment;
            //dr1["Principal"] = principal;
            //dtSchedule.Rows.Add(dr1);

            //int daysFirstPeriod = (lastDayOfMonth - lastDayOfMonth).Days;

            //DateTime d1 = DateTime.Now;
            //DateTime d2 = lastDayOfMonth;

            //TimeSpan t = d2 - d1;
            //decimal NoOfDays = decimal.Parse(t.TotalDays.ToString());
            //NoOfDays = Math.Round(NoOfDays, 2);
            ////decimal NoOfDays=double.Parse((d1 - d2).TotalDays);
            //decimal intrest = (NoOfDays + 1) / daysinyear * (intrestrate / 100) * principal;
            //dr["Interest"] = intrest;

            //for (int i = 0; i < numberofinstallments; i++)
            //{
            //    DataRow dr2 = dtSchedule.NewRow();
            //    dr2["DueDate"] = firstrepaymentdate.AddDays(7);
            //    dr2["Balance"] = principal + intrest;
            //    intrest = (NoOfDays + 1) / daysinyear * (intrestrate / 100) * principal;
            //    dr2["Interest"] = intrest;
            //    dr2["Repayment"] = repayment;
            //    dr2["Principal"] = principal;
            //    //dr["Interest"]= 
            //    dtSchedule.Rows.Add(dr2);

            //}


            dvSchedule.DataSource = dtSchedule;
            dvSchedule.DataBind();
        }
    }
}